package org.btik.light.mobile.ui.devs.camcar;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.*;
import androidx.annotation.Nullable;
import org.btik.context.SimpleContext;
import org.btik.light.meta.DevArea;
import org.btik.light.mobile.R;
import org.btik.light.mobile.common.handler.adaptor.SpeedHandler;
import org.btik.light.mobile.meta.VideoChannelType;
import org.btik.light.mobile.ui.devs.VideoCapability;
import org.btik.light.mobile.ui.factory.DevPanelActivity;
import org.btik.video.server.VideoServer;


import static org.btik.light.mobile.platform.conf.ContextGlobalNames.*;

public class CamCarActivity extends DevPanelActivity implements VideoCapability {
    private TextView speedShow;
    private SeekBar seekBar;

    private Switch videoSwitch;

    private CamCarActionMeta camCarActionMeta;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cam_car);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        bindAction();
        loadVideo();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void bindAction() {
        DevArea devArea = defaultDevMo.getDevArea();
        camCarActionMeta = devArea == DevArea.LOCAL ? CamCarHandlers.LOCAL_META : CamCarRawCommandHandlers.UDP_RAW_META;
        speedShow = findViewById(R.id.speed);

        SpeedHandler speedHandler = camCarActionMeta.getSpeedHandler();
        speedHandler.setSpeedShow(this::setSpeedShow);
        seekBar = bind(R.id.speed_bar, speedHandler);
        setSpeedShow(SpeedHandler.MAX_SPEED, true);
        bind(R.id.speed_up, speedHandler::speedUp);
        bind(R.id.speed_down, speedHandler::speedDown);
        bind(R.id.maxSpeed, speedHandler::goPikachu);
        bind(R.id.up_, camCarActionMeta.getGoForward());
        bind(R.id.left, camCarActionMeta.getToTurnLeft());
        bind(R.id.right, camCarActionMeta.getToTurnRight());
        bind(R.id.down, camCarActionMeta.getGoBackward());
        bind(R.id.stop, camCarActionMeta.getToStop());

        bind(R.id.light_switch, camCarActionMeta.getLightSwitch());
        bind(R.id.v_flip, camCarActionMeta.getVerticalFlip());
        bind(R.id.h_mirror, camCarActionMeta.getHorizontalMirrorSwitch());

    }

    private void setSpeedShow(int progress, boolean setSeekBar) {
        speedShow.setText(String.valueOf(progress));
        if (setSeekBar) {
            seekBar.setProgress(progress);
        }
    }

    private void loadVideo() {
        videoSwitch = bind(R.id.video_switch, camCarActionMeta.getVideoSwitch());
        SurfaceView carVideo = findViewById(R.id.video);
        bindChannel(carVideo.getHolder(),defaultDevMo.getVideoChannelType());
        videoSwitch.setVisibility(View.VISIBLE);
        carVideo.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onDestroy() {
        camCarActionMeta.getVideoOff().accept(videoSwitch);
        super.onDestroy();
    }

}
